import React, { Component } from "react"

const ChooserHoc = WrappedComponent => {

  class hoc extends Component {

    constructor(props) {
      super(props);
      this.state = {
        companyValue: [],
        selectedValue: [],
      };
    }

    setSource = (value, name) => {
      if (name === 'companyValue') {
        this.setState({
          companyValue: value,
          selectedValue: value.map(item => ({ name: item.label, id: item.key }))
        })
      } else {
        this.setState({
          selectedValue: value,
          companyValue: value.map(item => ({ label: item.name, key: item.id }))
        })
      }
    };

    render() {
      const { companyValue, selectedValue } = this.state;
      return (
        <WrappedComponent
          companyValue={companyValue}
          selectedValue={selectedValue}
          setSource={this.setSource}
          {...this.props}
        />
      )
    }

  }

  return hoc

};

export default ChooserHoc
